/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.posplus.losspreventiondataimporter.importer;

import com.posplus.lossprevention.APDAOFactory;
import com.posplus.lossprevention.mapping.Departments;
import com.posplus.lossprevention.mapping.Products;
import com.posplus.strack.mapping.Product;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import com.posplus.losspreventiondataimporter.service.DepartmentsService;
import com.posplus.losspreventiondataimporter.service.ProductService;
import com.posplus.losspreventiondataimporter.service.ProductsService;
import com.posplus.losspreventiondataimporter.util.ListToMap;
import org.apache.log4j.Logger;

/**
 *
 * @author scottw
 */
public class ProductsImporter {
    private APDAOFactory lpFactory;
    private ProductService productService;
    
    public ProductsImporter(ProductService productService, APDAOFactory lpFactory){
        this.lpFactory = lpFactory;
        this.productService = productService;
    }
    
    public void dataImport(){
        List<Product> products = productService.getAll();
        mapAndPersist(products, lpFactory);
    }
    
    private static void mapAndPersist(List<Product> products, APDAOFactory lpFactory){
        List<Products> mappedProducts = new ArrayList<Products>();
        DepartmentsService departmentService = new DepartmentsService(lpFactory);
        Map<Integer, Departments> departments = ListToMap.departmentsToMap(departmentService.getAll());
        
        
        Iterator<Product> iter = products.iterator();
        while (iter.hasNext()) {
            Product next = iter.next();
            
            try{
            Departments department = departments.get(next.getDepartment().intValue());
            if(department == null)
                throw new RuntimeException("Department " + next.getDepartment().toString() + " does not exist.");
            Products product = new Products(departments.get(next.getDepartment().intValue()), next.getUpc(), next.getDescription(), null);
            mappedProducts.add(product);
            }catch(RuntimeException ex){
                Logger.getLogger(ProductsImporter.class).debug(ex);
                Departments department = new Departments(next.getDepartment(), 0, "AUTO CREATED DEPARTMENT", null, null);
                Logger.getRootLogger().debug("Creating new department: " + department);
                departmentService.save(department);
                departments = ListToMap.departmentsToMap(departmentService.getAll());
                Products product = new Products(department, next.getUpc(), next.getDescription(), null);
                mappedProducts.add(product);
            }
        }
        
        if(mappedProducts.size() > 0){
            ProductsService productsService = new ProductsService(lpFactory);
            //productsService.bulkInsert(mappedProducts);
	    productsService.saveAll(mappedProducts);
        }
    }
}
